package com.kaikai.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.commonutils.JwtUtils;
import com.kaikai.commonutils.MD5;
import com.kaikai.educenter.entity.UcenterMember;
import com.kaikai.educenter.entity.vo.LoginVo;
import com.kaikai.educenter.entity.vo.RegisterVo;
import com.kaikai.educenter.mapper.UcenterMemberMapper;
import com.kaikai.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaikai.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-29
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;

    @Override
    public String login(LoginVo loginVo) {
        //非空判断
        if(StringUtils.isEmpty(loginVo.getPassword())||StringUtils.isEmpty(loginVo.getMobile())){
            throw new MyException(20001,"登陆失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile", loginVo.getMobile());
         UcenterMember phoneMember = this.getOne(wrapper);
        if(phoneMember==null){
            throw new MyException(20001,"用户不存在");
        }
        //判断填入的密码加密后是否和数据库中相同
        String MdPassword= MD5.encrypt(loginVo.getPassword());
        System.out.println(MdPassword);

        if(!phoneMember.getPassword().equals(MdPassword)){
            throw new MyException(20001,"账号或密码错误");
        }


        //用户账号和密码正确,返回token值
        String id=phoneMember.getId();
        String nickname=phoneMember.getNickname();
        return JwtUtils.getJwtToken(id,nickname);

    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        String code=registerVo.getCode();
        String nickName=registerVo.getNickname();
        String password=registerVo.getPassword();
        String mobile = registerVo.getMobile();
        //1.非空判断
        if(StringUtils.isEmpty(code)||
                StringUtils.isEmpty(nickName)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(mobile)
        ){
            throw new MyException(20001,"注册失败");
        }
        //2.校验验证码
//        if (){}
        //3.校验手机号
        int count = this.count(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if(count>0){
            throw new MyException(20001,"改手机号已被注册");
        }
        //4.存入数据库
        UcenterMember ucenterMember=new UcenterMember();
        ucenterMember.setAvatar("https://edu1998.oss-cn-beijing.aliyuncs.com/2021/05/13/f4333172274e4d74891a5a3c71c923e7file.png");
        ucenterMember.setNickname(nickName);
        ucenterMember.setPassword(password);
        ucenterMember.setMobile(mobile);

        this.save(ucenterMember);

    }

    @Override
    public Integer getRegisterCountByDay(String day) {
      return  baseMapper.getRegisterCountByDay(day);
    }
}
